package com.xiaoyu.awakening.common.core.dto;

import com.xiaoyu.awakening.common.core.exception.status.CommonReturnCode;
import com.xiaoyu.awakening.common.core.exception.status.IReturnCode;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * 返回消息
 *
 * @author june
 */
@Data
public class Result<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = -4569216979022946969L;
    private T data;

    private boolean isSuccess = false;

    private String resultMsg = CommonReturnCode.C999999.getMessage();

    private String code = CommonReturnCode.C999999.getCode();

    public Result() {
    }

    public static <T> Result<T> fail() {
        return new Result<>();
    }

    public static <T> Result<T> fail(String msg) {
        Result<T> result = fail();
        result.setResultMsg(msg);
        return result;
    }

    public static <T> Result<T> fail(IReturnCode returnCode) {
        Result<T> result = fail();
        result.setResultMsg(returnCode.getMessage());
        result.setCode(returnCode.getCode());
        return result;
    }

    public static <T> Result<T> fail(IReturnCode returnCode, String userDefinedMessage) {
        Result<T> result = fail();
        result.setResultMsg(userDefinedMessage);
        result.setCode(returnCode.getCode());
        return result;
    }

    public static <T> Result<T> success(String msg) {
        Result<T> result = success();
        result.setResultMsg(msg);
        result.setCode(CommonReturnCode.C000000.getCode());
        return result;
    }


    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setResultMsg("执行成功！");
        result.setCode(CommonReturnCode.C000000.getCode());
        result.setSuccess(true);
        return result;
    }

    public static <T> Result<T> success(T t) {
        Result<T> result = success();
        result.setData(t);
        result.setSuccess(true);
        result.setResultMsg(CommonReturnCode.C000000.getMessage());
        result.setCode(CommonReturnCode.C000000.getCode());
        return result;
    }

    public static <T> Result<T> success(T t, String message) {
        Result<T> result = success();
        result.setData(t);
        result.setSuccess(true);
        result.setResultMsg(message);
        result.setCode(CommonReturnCode.C000000.getCode());
        return result;
    }

    public static <T> Result<T> success(Result<T> result) {
        if (result == null) {
            return success();
        }
        result.setResultMsg(CommonReturnCode.C000000.getMessage());
        result.setCode(CommonReturnCode.C000000.getCode());
        return result;
    }
}
